package edu.unl.bsm.CoolingLoad.ErrorTesting;

import java.util.Arrays;
import java.util.List;


import edu.unl.bsm.CoolingLoad.Dao.ARCHIVE_DBDaoImpl;
import edu.unl.bsm.CoolingLoad.Dao.CoolingLoadDBDaoImpl;
import edu.unl.bsm.CoolingLoad.Util.Utility;

public class ErrorTest {
	
	/**
	 * check the outlier based on RMSE value. If the difference between recorded and predicted value is over  as twice as RMSE
	 * send the outlier to the manager 
	 */
	public static String checkOutlier(){
		ARCHIVE_DBDaoImpl dao = ARCHIVE_DBDaoImpl.getInstance();
		CoolingLoadDBDaoImpl ldao = CoolingLoadDBDaoImpl.getInstance();
		float currentRecordedCL = dao.getCurrentRecordedCLData();
		List<Object[]> currentPredictionCL = ldao.getPredictionForCurrentHour();
		int outLierCounter = 0;
		StringBuilder sb = new StringBuilder();
		for(Object[] element: currentPredictionCL){
			if((int)element[4] == 0){
				continue;
			}
			double error = currentRecordedCL - (double)element[3];
			if(Math.abs(error) > 2 * (double)element[5]){
				System.out.println("Outlier: " + Arrays.toString(element));
				sb.append("Outlier: ");
				sb.append(Arrays.toString(element));
				sb.append("\n");
				outLierCounter++;
			}
		}
		
		
		if(outLierCounter > 0){
			System.out.println("The number of OutLier is " + outLierCounter);
		}else{
			System.out.println("No Outlier");
		}
//		Utility.printArrayList(currentPredictionCL);
//		System.out.println("Recorded value: " + currentRecordedCL);
		
		return sb.toString();
	}
	

}
